package com.sky.controller.admin;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "商家菜品部门")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //清理缓存数据
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        dishService.save(dishDTO);
        //清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO){
       PageResult pageResult=dishService.page(dishPageQueryDTO);
       return Result.success(pageResult);
    }
    @DeleteMapping
    public Result delectId(@RequestParam List<Long>ids){
        dishService.delectId(ids);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectId(@PathVariable Long id){
        DishVO dishVO=dishService.selectid(id);
        return Result.success(dishVO);
    }
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        cleanCache("dish_*");
        return Result.success();
    }
    @PostMapping("/status/{status}")
    public Result inster(@PathVariable Integer status,Long id){
        dishService.inster(status,id);
        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(Long categoryId){
       List<DishVO> dishVOList=dishService.listid(categoryId);
       return Result.success(dishVOList);

    }



}
